package com.bases {
	import com.Event.RadioSelectEvent;
	import com.Interface.IItem;
	import com.Interface.IList;
	import com.Interface.IRadioData;
	import com.Interface.ITRadioCellRender;
	import flash.events.Event;
	import flash.text.TextFormat;

	/**
	 * 选择发生改变
	 */
	[Event(name="RADIO_SELECT_EVENT",type="com.Event.RadioSelectEvent")]
	/**
	 * 多选按钮
	 * @author pethan
	 *
	 */
	public class TRadioButtonList extends TUIComponent implements IList {
		private var radioList:MyList;

		public function TRadioButtonList(){
			super();
			radioList = new MyList();
			radioList.item = TRadioCellRender;
			this.addChild(radioList);
			this.addEventListener(RadioSelectEvent.RADIO_SELECT_EVENT, onRadioEvent);
			this.relayout();
		}

		/**
		 * 设置列表项的视图
		 * @param value 视图类CellItem
		 *
		 */
		public function set item(value:Class):void {
			this.radioList.item = value;
		}

		public function removeAllItem():void {
			this.radioList.removeAllItem();
		}

		public function set col(value:int):void {
			this.radioList.col = value;
		}

		public function addItem(value:*, needRelayout:Boolean = true, isUnshift:Boolean = false):void {
			this.radioList.addItem(value);
		}

		public function invalist():void {
			this.radioList.invalist();
		}

		override protected function relayout():void {
			radioList.setSize(200, 150);
		}

		override public function setSize(w:Number, h:Number):void {
			this.radioList.setSize(w, h);
		}

//		private function onRadioEvent(event:RadioSelectEvent):void{
//			for(var i:int=0;i<this.radioList.length;i++){
//				if(this.radioList.items[i]!=event.radioTarget){
//					(event.radioTarget as ITRadioCellRender).select=false;
//				}
//			}
//		}
		private function onRadioEvent(event:RadioSelectEvent):void {
			for (var i:int = 0; i < this.radioList.length; i++){
				if (this.radioList.items[i] != event.radioTarget){
					(this.radioList.items[i] as ITRadioCellRender).select = false;
				}
			}
		}

		/**
		 * 获取被选中的字符
		 * @return
		 *
		 */
		public function getSelectText():String {
			var ret:String = '';
			for (var i:int = 0; i < radioList.items.length; i++){
				if ((radioList.items[i] as ITRadioCellRender).select){
					ret = ((radioList.items[i] as IItem).userData as IRadioData).getText();
				}
			}
			return ret;
		}

		/**
		 * 获取被选中的编号
		 * @return
		 *
		 */
		public function getSelectID():String {
			var ret:String = '';
			for (var i:int = 0; i < radioList.items.length; i++){
				if ((radioList.items[i] as ITRadioCellRender).select){
					ret = ((radioList.items[i] as IItem).userData as IRadioData).getID();
				}
			}
			return ret;
		}

		/*把所有选项,不选中*/
		private function noSelect(item:*, index:int, arr:Array):void {
			(item as TRadioCellRender).select = false;
		}

		/**
		 * 设置默认选中项
		 * @param index
		 *
		 */
		public function set defaultIndex(index:int):void {
			radioList.items.forEach(noSelect);
			if (this.radioList.items.length != 0){
				(radioList.items[index] as ITRadioCellRender).select = true;
			}
		}

		/**
		 * 找到相同的,设为可选
		 * @param text
		 *
		 */
		public function set selectText(text:String):void {
			radioList.items.forEach(noSelect);
			var isSet:Boolean = false;
			for (var i:int = 0; i < radioList.items.length; i++){
				if (((radioList.items[i] as IItem).userData as IRadioData).getText() == text){
					(radioList.items[i] as ITRadioCellRender).select = true;
					isSet = true;
				}
			}
			if (!isSet){
				this.defaultIndex = 0;
			}
		}

		/**
		 * 用编号去找到相同的,设为可选
		 * @param text
		 *
		 */
		public function set selectID(text:String):void {
			radioList.items.forEach(noSelect);
			var isSet:Boolean = false;
			for (var i:int = 0; i < radioList.items.length; i++){
				if (((radioList.items[i] as IItem).userData as IRadioData).getID() == text){
					(radioList.items[i] as TRadioCellRender).select = true;
					isSet = true;
				}
			}
			if (!isSet){
				this.defaultIndex = 0;
			}
		}

		public function get counts():int {
			return this.radioList.items.length;
		}

		/**
		 * 设置列表项的字体大小
		 * @param size
		 *
		 */
		public function setFontSize(size:int):void {
			for (var i:int = 0; i < this.radioList.length; i++){
				(radioList.items[i] as TRadioCellRender).setFontSize(12);
			}
		}

		public function setFormat(format:TextFormat):void {
			for (var i:int = 0; i < this.radioList.length; i++){
				(radioList.items[i] as TRadioCellRender).setFormat(format);
			}
		}

		/**
		 * 重设置每个列表项目的大小,并且重新部局(todo:以后这个自动来处理)
		 * @param w 宽度
		 * @param h 高度
		 *
		 */
		public function setItemSize(w:Number, h:Number):void {
			for (var i:int = 0; i < this.radioList.length; i++){
				(radioList.items[i] as TRadioCellRender).setSize(w, h);
			}
			this.radioList.invalist();
		}
	}
}